package com.teensoft.core.user.imp;

import java.util.List;

import com.teensoft.base.dao.imp.HibernateDAO;
import com.teensoft.base.exception.DAOException;
import com.teensoft.core.user.Group;
import com.teensoft.core.user.GroupDAO;
import com.teensoft.core.user.common.Utils;

public class GroupDAOImp extends HibernateDAO implements GroupDAO {

	public void addGroup(Group o) throws DAOException {
		this.saveOrUpdate(o);
	}

	public void deleteGroupById(String id) throws DAOException {
		this.delete(Group.class, id);
	}

	public Group getById(String id) throws DAOException {
		return (Group)this.get(Group.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Group> getChild(String id) throws DAOException {
		String hql = "from parent = '"+id+"'";
		List<Group> result = this.getList(hql);
		return result;
	}

	public Group getParent(String id) throws DAOException {
		String pid = this.getById(id).getParent();
		if(!Utils.isNull(pid)){
			return this.getById(pid);
		}
		return null;
	}

	public void updateGroup(Group o) throws DAOException {
		this.saveOrUpdate(o);
	}

}
